// LOG BUTTONS (and behaviour)

// given a number, if it is smaller than 10, add a 0 in front of that number
// ie 7 -> 07
function _normalizeNumber(number){
	try{
		if (number < 10){
			number = '0' + number
		}
		return number
	}catch(ex){
		alert('exception in logMessage ' + ex)
		return number
	}
}

// compute and return the date in the format hours:minutes:seconds
function _getDate(){
	try{
		//alert('getting the time for the Logger')
		var now = new Date()
		var hour = _normalizeNumber(now.getHours())
		var minutes = _normalizeNumber(now.getMinutes())
		var seconds = _normalizeNumber(now.getSeconds())

		var timestamp = '[' + hour + ':' + minutes + ':' + seconds + '] ';
		//alert('timestamp is : ' + timestamp)
		return timestamp
	}catch(ex){
		alert('exception in _getDate ' + ex)
		return 'unknown '
	}
	
}

// given a string, log it into the history with a timestamp
function logMessage(messageString){
	try{
		_keepLastMessages() // delete old messages
		var history = prefManager.getCharPref("extensions.peercafe.logHistory");
		var newLog = _getDate() + messageString + '\n';
		history = history + newLog
		//alert('logging message: ' + newLog)
		prefManager.setCharPref("extensions.peercafe.logHistory", history);
	}catch(ex){
		alert('exception in logMessage ' + ex)
	}
}

// prune the message history in order to keep the last X messages
// X is the pcHistoryMaxLength (default 40, but may be changed by the user)
function _keepLastMessages(){
	var pcHistoryMaxLength = prefManager.getIntPref("extensions.peercafe.logHistoryLength");
	var history = prefManager.getCharPref("extensions.peercafe.logHistory");
	var historyArray = history.split('\n');
	var historyLength = historyArray.length
 	if (historyLength > pcHistoryMaxLength){ // the size of the history after adding the next item
		var lastestHistoryString = "" // will contain the latest pcHistoryMaxLength -1 items
		// I have to get the latest pcHistoryLength -1 items and return them as string
		var i;
		for (i = (historyLength - 1) - (pcHistoryMaxLength - 1); i < historyLength -1; i++){
			// copy everything except historyArray[0]
			lastestHistoryString += historyArray[i] + '\n'
		}
		lastestHistoryString += historyArray[i]
		prefManager.setCharPref("extensions.peercafe.logHistory", lastestHistoryString);
	}
}

// refresh the history box
// useful even if there is a sync with the prefs??
function refreshPcHistory() {
	try{
		//alert('refreshing the log history')
		//logMessage("refreshing the log history");
		var history = prefManager.getCharPref("extensions.peercafe.logHistory");
		try{
			var logBox = document.getElementById("pc_log");
			logBox.value = history;
		}catch(ex){
			//alert('pc_log text box not visible')
		}
	} catch(ex){
		alert('exception in refreshPcHistory ' + ex)
	}
}

// clear the history
function clearPcHistory() {
	try{
		//alert('clearing the log history')
		prefManager.setCharPref("extensions.peercafe.logHistory", "");
		//logMessage('clearing the history');
	} catch(ex){
		alert('exception in clearPcHistory ' + ex)
	}
}

// setter for the preference about the history length
function setHistoryLength(newSize){
	var oldSize; // for safety reasons (see catch)
	try{
		oldSize = prefManager.getIntPref("extensions.peercafe.logHistoryLength");
		if (newSize < 1){
			newSize = 40; // default
		}
		prefManager.setIntPref("extensions.peercafe.logHistoryLength", newSize);
	}catch(ex){
		alert('exception in setHistoryLength ' + ex)
		prefManager.setIntPref("extensions.peercafe.logHistoryLength", oldSize);
	}
}
